<?php

// Namespace

namespace Loop;


/**
 * CStringManager is a class that handles management of translated strings
 */

class CStringManager
{
	/**
	 * Gets the translation of a string
	 *
	 * @param	string 	$stringName		The name of the string
	 *
	 * @return 	string 	The translation (or $stringName if no translation found)
	 */
	
	public function getString($stringName)
	{
		// Get the string value
		
		$stringValue = v('STRING::' . $stringName);
		
		
		// If no value, use the string name. At least we'll have something to show
		
		if (is_null($stringValue) === true)
		{
			$stringValue = $stringName;
		}
		
		
		return $stringValue;
	}
	
	
	/**
	 * Initializes the string manager
	 */
	
	public function initialize()
	{
		// Scan the directory that contains strings for core constants
		
		$pathsToExtensions = app()->getExtensionManager()->getPathsToExtensions();
		
		foreach ($pathsToExtensions as $pathToExtension)
		{
			$pathToFiles = app()->getFileHelper()->listFiles($pathToExtension . 'String/Constant/', '*_' . pref('[Loop]/localization/languageIsoCode') . '.php');
			
			foreach ($pathToFiles as $pathToFile)
			{
				require_once($pathToFile);
			}
		}
		
		
		// Get common strings
		
		foreach ($pathsToExtensions as $pathToExtension)
		{
			$pathToFile = $pathToExtension . 'String/Common_' . pref('[Loop]/localization/languageIsoCode') . '.php';
			
			if (file_exists($pathToFile) == true)
			{
				require_once($pathToFile);
			}
		}
	}
	
	
	/**
	 * Sets a translated string
	 *
	 * @param 	string 	$stringName		The name of the string
	 * @param 	string 	$stringValue 	The value of the string
	 */
	
	public function setString($stringName, $stringValue)
	{
		v('STRING::' . $stringName, $stringValue);
	}
}

?>
